package cn.lj.org.service.impl;

import cn.lj.base.service.impl.BaseServiceImpl;
import cn.lj.org.domain.Department;
import cn.lj.org.mapper.DepartmentMapper;
import cn.lj.org.query.DepartmentQuery;
import cn.lj.org.service.IDepartmentService;
import cn.lj.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service/*交给spring管理*/
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)/*添加事务*/
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    /*注入mapper*/
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional
    public void add(Department d) {
        departmentMapper.save(d);
        if(d.getParent_id()==null){
            Long id = d.getId();
            d.setDirPath("/"+id);
        }else{
            Long parent_id = d.getParent().getId();
            Department department = departmentMapper.loadById(parent_id);
            String dirPath = department.getDirPath();
            d.setDirPath(dirPath+"/"+d.getId());
        }
        /*更新*/
        departmentMapper.update(d);
    }
    @Override
    @Transactional
    public void upadte(Department d) {
        if(d.getParent_id()==null){
            Long id = d.getId();
            d.setDirPath("/"+id);
        }else{
            Long parent_id = d.getParent().getId();
            Department department = departmentMapper.loadById(parent_id);
            String dirPath = department.getDirPath();
            d.setDirPath(dirPath+"/"+d.getId());
        }
        /*更新*/
        departmentMapper.update(d);
    }
    @Override
    @Transactional/*查询部门及其父类部门*/
    public List<Department> getTree() {
        return departmentMapper.loadTree();
    }

}
